package org.mobile.makibis;

import org.json.JSONObject;
import org.mobile.makibis.species.DataGetter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

public class ViewSpecies extends Activity{
	
	private WebView speciesDesc;
	public static int speciesID;
	final MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
	
	/*public ViewSpecies() {
		speciesDesc = (WebView)findViewById(R.id.speciesDesc);
        
        speciesDesc.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
 	    speciesDesc.loadUrl("file:///android_asset/species.html");
        
 	    speciesDesc.getSettings().setPluginsEnabled(true);
		speciesDesc.getSettings().setAllowFileAccess(true);
		speciesDesc.getSettings().setJavaScriptEnabled(true);
		
		try{
			String makibisURL = getResources().getString(R.string.feed_url);
			DataGetter getter = new DataGetter(makibisURL);
			JSONObject data = getter.getSpeciesData(speciesID);
			speciesDesc.loadUrl("javascript:loadSpeciesInfo(\"" + data.getString("vernacularName") + "\",\"" + data.getString("reference") + "\",\"" + data.getString("sex") + "\",\"" + data.getString("characteristic") + "\",\"" + data.getString("threatStatus") + "\",\"" + data.getString("distribution") + "\",\"" + data.getString("country") + "\",\"" + data.getString("continent") + "\",\"" + data.getString("longitude") + "\",\"" + data.getString("latitude") +  "\")");
			speciesDesc.loadUrl("javascript:loadSpeciesTaxonomy(\""+ data.getString("domain")+ "\",\"" + data.getString("kingdom")+ "\",\"" + data.getString("phylum")+ "\",\"" + data.getString("class") + "\",\"" + data.getString("order")+ "\",\"" + data.getString("family")+ "\",\"" + data.getString("genus")+ "\",\"" + data.getString("subgenus") + "\",\"" + data.getString("species") + "\",\"" + data.getString("subspecies") +  "\")");
			speciesDesc.setVisibility(View.VISIBLE);
		}
		catch(Exception e){}
	}*/
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        Log.d("oncreate", "oncreate");
        speciesDesc = (WebView)findViewById(R.id.speciesDesc);
        
        speciesDesc.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
 	    speciesDesc.loadUrl("file:///android_asset/species.html");
        
 	    speciesDesc.getSettings().setPluginsEnabled(true);
		speciesDesc.getSettings().setAllowFileAccess(true);
		speciesDesc.getSettings().setJavaScriptEnabled(true);
		
		try{
			Log.d("try", "try");
			String makibisURL = getResources().getString(R.string.feed_url);
			DataGetter getter = new DataGetter(makibisURL);
			JSONObject data = getter.getSpeciesData(speciesID);
			speciesDesc.loadUrl("javascript:loadSpeciesInfo(\"" + data.getString("vernacularName") + "\",\"" + data.getString("reference") + "\",\"" + data.getString("sex") + "\",\"" + data.getString("characteristic") + "\",\"" + data.getString("threatStatus") + "\",\"" + data.getString("distribution") + "\",\"" + data.getString("country") + "\",\"" + data.getString("continent") + "\",\"" + data.getString("longitude") + "\",\"" + data.getString("latitude") +  "\")");
			speciesDesc.loadUrl("javascript:loadSpeciesTaxonomy(\""+ data.getString("domain")+ "\",\"" + data.getString("kingdom")+ "\",\"" + data.getString("phylum")+ "\",\"" + data.getString("class") + "\",\"" + data.getString("order")+ "\",\"" + data.getString("family")+ "\",\"" + data.getString("genus")+ "\",\"" + data.getString("subgenus") + "\",\"" + data.getString("species") + "\",\"" + data.getString("subspecies") +  "\")");
			speciesDesc.setVisibility(View.VISIBLE);
		}
		catch(Exception e){}
	 }
	
	 public class MyJavaScriptInterface {
		Context mContext;
		
		MyJavaScriptInterface(Context c) {
			mContext = c;
		}
	}
	    
}
